/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package csvconvertor;

import java.io.*;
import java.util.ArrayList;

/**
 *
 * @author sajin
 */
public class CSVReader {

    public CSVReader(String fileName) throws FileNotFoundException {
        csvFile = new FileReader(fileName);
        csvReader = new BufferedReader(csvFile);
        readAllLines = false;

        String headerLine = readLineFromCSV();
        readHeaders(headerLine);
    }

    @Override
    protected void finalize() throws Throwable {
        csvFile.close();
        csvReader.close(); 
    }

    protected boolean UnbalancedQuotes(String line) {
        boolean uQuotes = false;
        char[] lineArray = line.toCharArray();
        
        for (char chr: lineArray )
        {
            if ( chr == '"')
                uQuotes = !uQuotes;
        }

        return uQuotes;
    }

    protected String readLineFromCSV() {
        String line,presentLine;
        boolean doneReading = false;

        line = new String();
        presentLine = new String();
        line = "" ;

          try {
            while ( !doneReading && ( presentLine = csvReader.readLine() ) != null ) {
                line += presentLine;
                if ( UnbalancedQuotes(line))
                    // More to read in the line.
                    doneReading = false;
                else
                    // Nothing more to read.
                    doneReading = true;
            }
            if ( presentLine == null)
                readAllLines = true;
        }
        catch ( IOException iexp ) {
            System.err.println("Error reading line:" + iexp.getMessage() ) ;
        }
        return line;
    }

    protected void readHeaders(String headerLine) {
        csvFields = parseLine(headerLine);
    }

    protected String[] parseLine(String line) {
        ArrayList Tokens = new ArrayList();
        String[] strTokens;
        String token = "";
        char[] lineArray = line.toCharArray();
        boolean uQuotes = false;
        boolean endVariable = false;
    
        for ( char chr : lineArray ) {
            if(endVariable) {
                // Collection of variable into token
                // has ended.
                endVariable = false ;
                Tokens.add(token) ;
                token = "";
            }
            if ( chr == '"' ) {
                uQuotes = !uQuotes;
            }
            else if ( ( chr == ',') && !uQuotes ){
                // The variable has ended.
                endVariable = true;
            }
            else {
                token += chr; 
            }
        }
        Tokens.add(token);

        strTokens = new String[Tokens.size()];
        Tokens.toArray(strTokens) ;

        return strTokens;
    } 

    public String[] getHeaderFields() {
        return csvFields;
    }

    public String[] getFields() {
        if ( readAllLines )
            return null;
        else {
            String[] results = parseLine(readLineFromCSV());
            if ( readAllLines )
                return null ;
            else
                return results ;
        }
    }

    protected FileReader csvFile ;
    protected BufferedReader csvReader;
    protected String csvLine;
    protected String[] csvFields;
    protected boolean readAllLines;
}
